package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwbhT;
import cn.e4j.modular.crm.entity.GwbjT;
import cn.e4j.modular.crm.entity.GwbkT;
import cn.e4j.modular.crm.entity.GwblT;
import cn.e4j.modular.crm.mapper.GwbhTMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 拜访记录维护 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-04-03
 */
@Service
public class GwbhTServiceImpl extends ServiceImpl<GwbhTMapper, GwbhT> {

    @Autowired
    public SysAuthorityService sysAuthorityService;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwbhlist(Page<Object> page, Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));
//        List<Map<String, Object>> gwbhDtos = this.baseMapper.getGwbhlist(page, dto);
        List<Map<String, Object>> gwbhDtos = InvokeUtil.invokeBeanMethod("gwbhTMapper,getGwbhlist",page, dto);
        return gwbhDtos;
    }

    @Transactional(readOnly = true)
    public List<Dto> listGwbf(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        dto.put("GWBF001", shiroUser.getORG_ID());
        List<Dto> gwbfDtos = this.baseMapper.listGwbf(dto);
        return gwbfDtos;
    }

    public BigDecimal getGwb004(Dto inDto) {
        Dto result = this.baseMapper.getGwb004(inDto);
        if (result != null) {
            return result.getBigDecimal("GWBKSEQ");
        } else {
            return null;
        }
    }

    /**
     * 根据主题+客户获取已有的 商品明细
     */
    public List<Dto> gwat160Spmx(Dto inDto) {
        List<Dto> list = this.baseMapper.gwat160Spmx(inDto);
        return list;
    }

    @Transactional(readOnly = true)
    public List<Dto> listGwbc(Dto dto) {
        List<Dto> gwbcDtos = this.baseMapper.listGwbc(dto);
        return gwbcDtos;
    }

    @Transactional(readOnly = true)
    public Dto listGwbl(Dto dto) {
        if(dto.getString("GWBHID").equals("")){
            dto.put("GWBHID"," ");
        }
        if(dto.getString("GWBHNAME").equals("")){
            dto.put("GWBHNAME"," ");
        }
        Dto gwblDtos = this.baseMapper.listGwbl(dto);
        return gwblDtos;
    }

    @Transactional(readOnly = true)
    public List<Dto> listGwbj(Dto dto) {
        List<Dto> gwbjDtos = this.baseMapper.listGwbj(dto);
        return gwbjDtos;
    }

    @Transactional(readOnly = true)
    public Dto calc(Dto dto) {
        Dto resultDto = Dtos.newDto();
//        List<Dto> gwcsList = this.baseMapper.listGwcs(dto);
        List<Dto> gwcsList = InvokeUtil.invokeBeanMethod("gwbhTMapper,listGwcs",dto);
        int gwcs005 = 0;
        if (gwcsList.size() > 0) {
            for (int i = 0; i < gwcsList.size(); i++) {
                gwcs005 = gwcs005 + (gwcsList.get(i).getInteger("GWCS003") * gwcsList.get(i).getInteger("GWCS004"));
            }
        }
        List<Dto> gwbkList = this.getGwbkData(dto);
        int gwbk005 = 0;
        if (gwbkList.size() > 0) {
            for (int i = 0; i < gwbkList.size(); i++) {
                if(gwbkList.get(i).getInteger("GWBK003") != null && gwbkList.get(i).getInteger("GWBK004") != null){
                    gwbk005 = gwbk005 + (gwbkList.get(i).getInteger("GWBK003") * gwbkList.get(i).getInteger("GWBK004"));
                }
                //gwbk005 = gwbk005 + (gwbkList.get(i).getInteger("GWBK003") * gwbkList.get(i).getInteger("GWBK004"));
            }
        }
        resultDto.put("GWCS005", gwcs005);
        resultDto.put("GWBK005", gwbk005);
        return resultDto;
    }

    public List<Dto> getGwbkData(Dto inDto) {
        List<Dto> resultDto = new ArrayList<>();
        if (inDto.getString("TYPE").equals("")) {
//            resultDto = this.baseMapper.listGwbk1(inDto);
            resultDto = InvokeUtil.invokeBeanMethod("gwbhTMapper,listGwbk1",inDto);
        } else {
            if (inDto.getString("TYPE").equals("1")) {
//                resultDto = this.baseMapper.listGwbk(inDto);
                resultDto = InvokeUtil.invokeBeanMethod("gwbhTMapper,listGwbk",inDto);
            } else {
//                resultDto = this.baseMapper.listGwbk2(inDto);
                resultDto = InvokeUtil.invokeBeanMethod("gwbhTMapper,listGwbk2",inDto);
            }
        }
        return resultDto;
    }

    public List<Dto> listGwcs(Dto inDto) {
//        List<Dto> resultDto = this.baseMapper.listGwcs(inDto);
        List<Dto> resultDto =InvokeUtil.invokeBeanMethod("gwbhTMapper,listGwcs",inDto);
        return resultDto;
    }

    public List<Map<String, Object>> getGwbkData1(Page page, Dto inDto) {
        List<Map<String, Object>> resultDto = new ArrayList<>();
        if (inDto.getString("TYPE").equals("")) {
//            resultDto = this.baseMapper.listGwbk1Page(page, inDto);
            resultDto =InvokeUtil.invokeBeanMethod("gwbhTMapper,listGwbk1Page",page,inDto);
        } else {
            if (inDto.getString("TYPE").equals("1")) {
//                resultDto = this.baseMapper.listGwbkPage(page, inDto);
                resultDto =InvokeUtil.invokeBeanMethod("gwbhTMapper,listGwbkPage",page,inDto);
            } else {
//                resultDto = this.baseMapper.listGwbk2Page(page, inDto);
                resultDto =InvokeUtil.invokeBeanMethod("gwbhTMapper,listGwbk2Page",page,inDto);
            }
        }
        return resultDto;
    }

    public List<Map<String, Object>> listGwcs1(Page page, Dto inDto) {
//        List<Map<String, Object>> resultDto = this.baseMapper.listGwcsPage(page, inDto);
        List<Map<String, Object>> resultDto = InvokeUtil.invokeBeanMethod("gwbhTMapper,listGwcsPage",page,inDto);
        return resultDto;
    }

    public List<Dto> getGwbh(Dto inDto) {
//        List<Dto> resultDto = this.baseMapper.listGwbh(inDto);
        List<Dto> resultDto =InvokeUtil.invokeBeanMethod("gwbhTMapper,listGwbh",inDto);
        for (int i = 0; i < resultDto.size(); i++) {
            Dto pDto = Dtos.newDto();
            pDto.put("ENT", inDto.getString("ENT"));
            pDto.put("GWBHNAME", resultDto.get(i).getString("GWBHNAME"));
            pDto.put("GWBHID", resultDto.get(i).getString("GWBHID"));
            List<Dto> gwbl = this.baseMapper.listGwbl2(pDto);
            String gwbl_ = "";
            if (gwbl.size() > 0) {
                for (int j = 0; j < gwbl.size(); j++) {
                    gwbl_ = gwbl_ + gwbl.get(j).getString("GWBCL003");
                }
            }
            resultDto.get(i).put("GWBL", gwbl_);
            List<Dto> gwbj = this.baseMapper.listGwbj(pDto);
            String gwbj_ = "";
            if (gwbj.size() > 0) {
                for (int j = 0; j < gwbj.size(); j++) {
                    gwbj_ = gwbj_ + gwbj.get(j).getString("GWBJ001");
                }
            }
            resultDto.get(i).put("GWBJ", gwbj_);
        }
        return resultDto;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwbf(Dto dto) {
        List<Dto> gwbfDtos = this.baseMapper.listGwbf(dto);
        return gwbfDtos;
    }

    /**
     * 根据 主题+客户 获取商机拜访下一个序列号（最大序号+1)
     */
    public BigDecimal getGwb003(Dto inDto) {
        Dto result = this.baseMapper.getGwb003(inDto);
        if (result != null) {
            BigDecimal gwbhseq = result.getBigDecimal("GWBHSEQ");
            return gwbhseq.add(BigDecimal.ONE);
        } else {
            return BigDecimal.ONE;
        }
    }

    @Transactional
    public void addData(List<GwbkT> gwbkTList, List<GwblT> gwblTList, List<GwbhT> gwbhTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwbhT gwbhT = gwbhTList.get(0);
        Dto queryDto = Dtos.newDto();
        queryDto.put("ENT", shiroUser.getDBC());
        queryDto.put("GWBHNAME", gwbhT.getGWBHNAME());
        queryDto.put("GWBHID", gwbhT.getGWBHID());
        BigDecimal gwbhseq = this.getGwb003(queryDto);

        int seq = 1;
        for (GwbkT gwbkT : gwbkTList) {
            gwbkT.setGWBKENT(shiroUser.getDBC());
            gwbkT.setGWBKNAME(gwbhT.getGWBHNAME());
            gwbkT.setGWBKID(gwbhT.getGWBHID());
            gwbkT.setGWBKSEQ(new BigDecimal(seq));
            gwbkT.setGWBKSEQ2(gwbhseq);
            gwbkT.setGWBKACTI("Y");
            gwbkT.insert();
            seq++;
        }

        int count = 1;
        for (GwblT gwblT : gwblTList) {
            UpdateWrapper<GwblT> gwblTUpdateWrapper = new UpdateWrapper<>();
            gwblTUpdateWrapper.eq("GWBLENT", shiroUser.getDBC());
            gwblTUpdateWrapper.eq("GWBLNAME", gwbhT.getGWBHNAME());
            gwblTUpdateWrapper.eq("GWBLID", gwbhT.getGWBHID());
            gwblTUpdateWrapper.eq("GWBLSEQ", count);
            gwblT.delete(gwblTUpdateWrapper);

            gwblT.setGWBLENT(shiroUser.getDBC());
            gwblT.setGWBLNAME(gwbhT.getGWBHNAME());
            gwblT.setGWBLID(gwbhT.getGWBHID());
            gwblT.setGWBLSEQ(new BigDecimal(count));
            gwblT.setGWBLACTI("Y");
            gwblT.insert();
            count++;
        }

        gwbhT.setGWBHSEQ(gwbhseq);
        gwbhT.setGWBHENT(shiroUser.getDBC());
        gwbhT.setGWBH002(shiroUser.getID());
        gwbhT.setGWBHACTI("N");
        gwbhT.insert();
    }

    @Transactional
    public void editData(List<GwbkT> gwbkTList, List<GwblT> gwblTList, List<GwbhT> gwbhTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwbhT gwbhT = gwbhTList.get(0);

        GwbkT gwbkTDel = new GwbkT();
        UpdateWrapper<GwbkT> gwbkTUpdateWrapper = new UpdateWrapper<>();
        gwbkTUpdateWrapper.eq("GWBKENT", shiroUser.getDBC());
        gwbkTUpdateWrapper.eq("GWBKNAME", gwbhT.getGWBHNAME());
        gwbkTUpdateWrapper.eq("GWBKID", gwbhT.getGWBHID());
        gwbkTUpdateWrapper.eq("GWBKSEQ2", gwbhT.getGWBHSEQ());
        gwbkTDel.delete(gwbkTUpdateWrapper);

        int seq = 1;
        for (GwbkT gwbkT : gwbkTList) {
            gwbkT.setGWBKENT(shiroUser.getDBC());
            gwbkT.setGWBKNAME(gwbhT.getGWBHNAME());
            gwbkT.setGWBKID(gwbhT.getGWBHID());
            gwbkT.setGWBKSEQ(new BigDecimal(seq));
            gwbkT.setGWBKSEQ2(gwbhT.getGWBHSEQ());
            gwbkT.setGWBKACTI("Y");
            gwbkT.insert();
            seq++;
        }

        int count = 1;
        GwblT gwblTDel = new GwblT();
        UpdateWrapper<GwblT> gwblTUpdateWrapper = new UpdateWrapper<>();
        gwblTUpdateWrapper.eq("GWBLENT", shiroUser.getDBC());
        gwblTUpdateWrapper.eq("GWBLNAME", gwbhT.getGWBHNAME());
        gwblTUpdateWrapper.eq("GWBLID", gwbhT.getGWBHID());
        gwblTDel.delete(gwblTUpdateWrapper);
        for (GwblT gwblT : gwblTList) {
            gwblT.setGWBLENT(shiroUser.getDBC());
            gwblT.setGWBLNAME(gwbhT.getGWBHNAME());
            gwblT.setGWBLID(gwbhT.getGWBHID());
            gwblT.setGWBLSEQ(new BigDecimal(count));
            gwblT.setGWBLACTI("Y");
            gwblT.insert();
            count++;
        }

        gwbhT.setGWBHENT(shiroUser.getDBC());
        gwbhT.setGWBH002(shiroUser.getID());
        UpdateWrapper<GwbhT> gwbhTUpdateWrapper = new UpdateWrapper<>();
        gwbhTUpdateWrapper.eq("GWBHENT", shiroUser.getDBC());
        gwbhTUpdateWrapper.eq("GWBHSEQ", gwbhT.getGWBHSEQ());
        gwbhTUpdateWrapper.eq("GWBHNAME", gwbhT.getGWBHNAME());
        gwbhTUpdateWrapper.eq("GWBHID", gwbhT.getGWBHID());
        gwbhT.update(gwbhTUpdateWrapper);
    }

    @Transactional
    public void delete(GwbhT gwbhT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        UpdateWrapper<GwbhT> gwbhTUpdateWrapper = new UpdateWrapper<>();
        gwbhTUpdateWrapper.eq("GWBHENT", shiroUser.getDBC());
        gwbhTUpdateWrapper.eq("GWBHNAME", gwbhT.getGWBHNAME());
        gwbhTUpdateWrapper.eq("GWBHSEQ", gwbhT.getGWBHSEQ());
        gwbhTUpdateWrapper.eq("GWBHID", gwbhT.getGWBHID());
        gwbhT.delete(gwbhTUpdateWrapper);

        GwbkT gwbkT = new GwbkT();
        UpdateWrapper<GwbkT> gwbkTUpdateWrapper = new UpdateWrapper<>();
        gwbkTUpdateWrapper.eq("GWBKENT", shiroUser.getDBC());
        gwbkTUpdateWrapper.eq("GWBKNAME", gwbhT.getGWBHNAME());
        gwbkTUpdateWrapper.eq("GWBKSEQ2", gwbhT.getGWBHSEQ());
        gwbkTUpdateWrapper.eq("GWBKID", gwbhT.getGWBHID());
        gwbkT.delete(gwbkTUpdateWrapper);

        GwblT gwblT = new GwblT();
        UpdateWrapper<GwblT> gwblTUpdateWrapper = new UpdateWrapper<>();
        gwblTUpdateWrapper.eq("GWBLENT", shiroUser.getDBC());
        gwblTUpdateWrapper.eq("GWBLNAME", gwbhT.getGWBHNAME());
        gwblTUpdateWrapper.eq("GWBLID", gwbhT.getGWBHID());
        gwblT.delete(gwblTUpdateWrapper);

        GwbjT gwbjT = new GwbjT();
        UpdateWrapper<GwbjT> gwbjTUpdateWrapper = new UpdateWrapper<>();
        gwbjTUpdateWrapper.eq("GWBJENT", shiroUser.getDBC());
        gwbjTUpdateWrapper.eq("GWBJNAME", gwbhT.getGWBHNAME());
        gwbjTUpdateWrapper.eq("GWBJID", gwbhT.getGWBHID());
        gwbjT.delete(gwbjTUpdateWrapper);
    }

    @Transactional
    public void delDataBatch(List<GwbhT> gwbhTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for (GwbhT gwbhT : gwbhTList) {
            UpdateWrapper<GwbhT> gwbhTUpdateWrapper = new UpdateWrapper<>();
            gwbhTUpdateWrapper.eq("GWBHENT", shiroUser.getDBC());
            gwbhTUpdateWrapper.eq("GWBHNAME", gwbhT.getGWBHNAME());
            gwbhTUpdateWrapper.eq("GWBHSEQ", gwbhT.getGWBHSEQ());
            gwbhTUpdateWrapper.eq("GWBHID", gwbhT.getGWBHID());
            gwbhT.delete(gwbhTUpdateWrapper);

            GwbkT gwbkT = new GwbkT();
            UpdateWrapper<GwbkT> gwbkTUpdateWrapper = new UpdateWrapper<>();
            gwbkTUpdateWrapper.eq("GWBKENT", shiroUser.getDBC());
            gwbkTUpdateWrapper.eq("GWBKNAME", gwbhT.getGWBHNAME());
            gwbkTUpdateWrapper.eq("GWBKSEQ2", gwbhT.getGWBHSEQ());
            gwbkTUpdateWrapper.eq("GWBKID", gwbhT.getGWBHID());
            gwbkT.delete(gwbkTUpdateWrapper);

            GwblT gwblT = new GwblT();
            UpdateWrapper<GwblT> gwblTUpdateWrapper = new UpdateWrapper<>();
            gwblTUpdateWrapper.eq("GWBLENT", shiroUser.getDBC());
            gwblTUpdateWrapper.eq("GWBLNAME", gwbhT.getGWBHNAME());
            gwblTUpdateWrapper.eq("GWBLID", gwbhT.getGWBHID());
            gwblT.delete(gwblTUpdateWrapper);

            GwbjT gwbjT = new GwbjT();
            UpdateWrapper<GwbjT> gwbjTUpdateWrapper = new UpdateWrapper<>();
            gwbjTUpdateWrapper.eq("GWBJENT", shiroUser.getDBC());
            gwbjTUpdateWrapper.eq("GWBJNAME", gwbhT.getGWBHNAME());
            gwbjTUpdateWrapper.eq("GWBJID", gwbhT.getGWBHID());
            gwbjT.delete(gwbjTUpdateWrapper);
        }
    }

    @Transactional
    public void updateData(List<GwbhT> gwbhTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for (GwbhT gwbhT : gwbhTList) {
            UpdateWrapper<GwbhT> gwbhTUpdateWrapper = new UpdateWrapper<>();
            gwbhTUpdateWrapper.eq("GWBHENT", shiroUser.getDBC());
            gwbhTUpdateWrapper.eq("GWBHNAME", gwbhT.getGWBHNAME());
            gwbhTUpdateWrapper.eq("GWBHSEQ", gwbhT.getGWBHSEQ());
            gwbhTUpdateWrapper.eq("GWBHID", gwbhT.getGWBHID());
            gwbhT.update(gwbhTUpdateWrapper);
        }
    }

    @Transactional
    public void addGwbj(GwbjT gwbjT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwbjT.setGWBJENT(shiroUser.getDBC());
        gwbjT.insert();
    }

    @Transactional
    public void delGwbjData(List<GwbjT> gwbjTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for (GwbjT gwbjT : gwbjTList) {
            UpdateWrapper<GwbjT> gwbjTUpdateWrapper = new UpdateWrapper<>();
            gwbjTUpdateWrapper.eq("GWBJENT", shiroUser.getDBC());
            gwbjTUpdateWrapper.eq("GWBJNAME", gwbjT.getGWBJNAME());
            gwbjTUpdateWrapper.eq("GWBJSEQ", gwbjT.getGWBJSEQ());
            gwbjTUpdateWrapper.eq("GWBJID", gwbjT.getGWBJID());
            gwbjT.delete(gwbjTUpdateWrapper);
        }
    }

    public Dto getCount(Dto inDto) {
        Dto outDto = Dtos.newDto();
        ShiroUser user = ShiroKit.getUser();
        inDto.put("ent", user.getDBC());
        inDto.put("lang", LangUtils.getLang(user.getLANG()));
        inDto.put("ID", user.getID());
        //        业务员权限
        Dto sys004 = sysAuthorityService.getSys004(sysAuthorityService.getSys003(inDto));
        
        inDto.put("ywy",sys004.get("ywy"));
        inDto.put("GWBHENT", user.getDBC());
        inDto.put("type", "1");
        Dto dto = this.baseMapper.getCount(inDto);
        if (dto == null) {
            outDto.put("count1", 0);
        } else {
            outDto.put("count1", dto.getString("count"));
        }
        inDto.put("type", "2");
        dto = this.baseMapper.getCount(inDto);
        if (dto == null) {
            outDto.put("count2", 0);
        } else {
            outDto.put("count2", dto.getString("count"));
        }
        inDto.put("type", "3");
        dto = this.baseMapper.getCount(inDto);
        if (dto == null) {
            outDto.put("count3", 0);
        } else {
            outDto.put("count3", dto.getString("count"));
        }
        inDto.put("type", "4");
        dto = this.baseMapper.getCount(inDto);
        if (dto == null) {
            outDto.put("count4", 0);
        } else {
            outDto.put("count4", dto.getString("count"));
        }
        inDto.put("type", "5");
        dto = this.baseMapper.getCount(inDto);
        if (dto == null) {
            outDto.put("count5", 0);
        } else {
            outDto.put("count5", dto.getString("count"));
        }
        return outDto;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwbglist(Page<Object> page, Dto dto) {
        if("true".equals(dto.getString("permission"))){
            //部门权限
            dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));

            // 业务员权限
            Dto sys004= sysAuthorityService.getSys004(sysAuthorityService.getOrgsAuthority(dto));
            dto.put("ywy",sys004.get("ywy"));
        }
        List<Map<String, Object>> gwbgDtos = this.baseMapper.getGwbgList(page, dto);
        return gwbgDtos;
    }

    @Transactional(readOnly = true)
    public Dto getGwbh011(Dto dto) {
        Dto gwbhDtos = this.baseMapper.getGwbh011(dto);
        return gwbhDtos;
    }
}
